//
//  MWModifyPhoneController.m
//  XiRan-iOS
//
//  Created by liuyiming on 2019/10/31.
//  Copyright © 2019 Wood. All rights reserved.
//

#import "MWModifyPhoneController.h"
#import "MWCountryCodeController.h"
#import "JKCountDownButton.h"

@interface MWModifyPhoneController ()

@property(nonatomic, strong) UILabel *hintLB;
@property(nonatomic, strong) UIButton *phoneBtn;
@property(nonatomic, strong) UITextField *phoneTF;
@property(nonatomic, strong) UIView *phoneLineView;

@property(nonatomic, strong) UITextField *smsCodeTF;
@property(nonatomic, strong) JKCountDownButton *countDownButton;
@property(nonatomic, strong) UIView *smsCodeLineView;

@property(nonatomic, strong) UIButton *submitBtn;

@property(nonatomic, strong) NSString *countryCode;

@end

@implementation MWModifyPhoneController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = [[MWConfigManager manager] objectForKey:@"revisephone" defaultValue:@"修改手机号"];
    if ([MWConfigManager manager].isNetError) {
        self.errorTitleLabel.text = [[MWConfigManager manager] objectForKey:@"click_retry" defaultValue:@"点击重试"];
        self.errorTitleLabel.hidden = self.errorImageView.hidden = NO;
    } else {
        [self setupPageSubviews];
        [self layoutPageSubviews];
    }
}

- (void)refresh {
    [super refresh];
    if ([MWConfigManager manager].isNetError) {
        self.errorTitleLabel.hidden = self.errorImageView.hidden = NO;
    } else {
        self.errorTitleLabel.hidden = self.errorImageView.hidden = YES;
        [self setupPageSubviews];
        [self layoutPageSubviews];
    }
}

- (void)submitAction:(UIButton *)sender {
    NSString *phone = _phoneTF.text;
    NSString *captcha = _smsCodeTF.text;
    [self modifyPhone:phone captcha:captcha countryCode:_countryCode];
}

- (void)phoneAction:(UIButton *)sender {
    MWCountryCodeController *controller = [[MWCountryCodeController alloc] init];
    controller.isPresent = YES;
    MWWeakSelf
    controller.countryCodeBlock = ^(NSString *countryCode) {
        weakSelf.countryCode = countryCode;
        [weakSelf.phoneBtn setTitle:[NSString stringWithFormat:@"+%@", countryCode] forState:UIControlStateNormal];
        [weakSelf.phoneBtn setIconInRightWithSpacing:5];
    };
    [self presentViewController:controller animated:YES completion:^{
        
    }];
}

#pragma mark - 修改手机号验证码
- (void)loadSmsCodeWithPhone:(NSString *)phone countryCode:(NSString *)countryCode {
    NSMutableDictionary *paraDic = [[NSMutableDictionary alloc] init];
    [paraDic setObject:phone forKey:@"mobile"];
    [paraDic setObject:countryCode forKey:@"country_code"];
    [paraDic setObject:@"changemobile" forKey:@"event"];
    [paraDic setObject:[MWConfigManager manager].language forKey:@"language"];
    
    MWWeakSelf
    [[MWNetworkManager manager] POST:kSmsCodeUrl parameters:paraDic progress:^(NSProgress *downloadProgress) {
        
    } success:^(id responseObject) {
        NSDictionary *dic = responseObject;
        NSInteger code = [[dic objectForKey:@"code"] integerValue];
        NSString *msg = [dic objectForKey:@"msg"];
        
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.label.text = msg;
        [hud hideAnimated:YES afterDelay:2];
        
        if (code == 1) {
            weakSelf.countDownButton.enabled = NO;
            [weakSelf.countDownButton startCountDownWithSecond:60];
        }
        
    } failure:^(NSError *error) {
       
    }];
}

#pragma mark - 修改手机号
- (void)modifyPhone:(NSString *)phone captcha:(NSString *)captcha countryCode:(NSString *)countryCode {
    NSString *token = [MWUserManager manager].user.token;
    NSMutableDictionary *paraDic = [[NSMutableDictionary alloc] init];
    [paraDic setObject:token forKey:@"token"];
    [paraDic setObject:phone forKey:@"mobile"];
    [paraDic setObject:captcha forKey:@"captcha"];
    [paraDic setObject:countryCode forKey:@"country_code"];
    [paraDic setObject:[MWConfigManager manager].language forKey:@"language"];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    hud.label.text = [[MWConfigManager manager] objectForKey:@"loadin" defaultValue:@"加载中..."];
    
    MWWeakSelf
    [[MWNetworkManager manager] POST:kModifyPhoneUrl parameters:paraDic progress:^(NSProgress *downloadProgress) {
        
    } success:^(id responseObject) {
        NSDictionary *dic = responseObject;
        NSInteger code = [[dic objectForKey:@"code"] integerValue];
        NSString *msg = [dic objectForKey:@"msg"];
        
        hud.mode = MBProgressHUDModeText;
        hud.label.text = msg;
        [hud hideAnimated:YES afterDelay:2];
        
        if (code == 1) {
            [MWUserManager manager].user.mobile = weakSelf.phoneTF.text;
            [self.navigationController popViewControllerAnimated:YES];
        }
        
    } failure:^(NSError *error) {
        [hud hideAnimated:YES];
    }];
}

#pragma mark - UI
- (void)setupPageSubviews {
    _hintLB = [[UILabel alloc] init];
    _hintLB.font = MWBoldFont(14);
    _hintLB.textColor = MWHexColor(@"#333333");
    _hintLB.text = [[MWConfigManager manager] objectForKey:@"input_new_phone" defaultValue:@"请输入新的手机号码"];
    [self.view addSubview:_hintLB];
    
    _countryCode = @"86";
    _phoneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _phoneBtn.titleLabel.font = MWFont(14);
    [_phoneBtn setTitle:@"+86" forState:UIControlStateNormal];
    [_phoneBtn setTitleColor:MWHexColor(@"#1E1934") forState:UIControlStateNormal];
    [_phoneBtn setImage:[UIImage imageNamed:@"ic_arrow_down"] forState:UIControlStateNormal];
    [_phoneBtn addTarget:self action:@selector(phoneAction:) forControlEvents:UIControlEventTouchUpInside];
    [_phoneBtn setIconInRightWithSpacing:5];
    [self.view addSubview:_phoneBtn];
    
    _phoneTF = [[UITextField alloc] init];
    _phoneTF.font = MWFont(12);
    _phoneTF.textColor = MWHexColor(@"#BAC2CE");
    _phoneTF.placeholder = [[MWConfigManager manager] objectForKey:@"input_phone" defaultValue:@"请输入手机号码"];
    _phoneTF.text = [MWUserManager manager].user.mobile;
    [self.view addSubview:_phoneTF];
    
    
    _phoneLineView = [[UIView alloc] init];
    _phoneLineView.backgroundColor = MWHexColor(@"#D8D8D8");
    [self.view addSubview:_phoneLineView];
    
    _smsCodeTF = [[UITextField alloc] init];
    _smsCodeTF.font = MWFont(12);
    _smsCodeTF.textColor = MWHexColor(@"#BAC2CE");
    _smsCodeTF.placeholder = [[MWConfigManager manager] objectForKey:@"input_code" defaultValue:@"请输入验证码"];
    [self.view addSubview:_smsCodeTF];
    
    _smsCodeLineView = [[UIView alloc] init];
    _smsCodeLineView.backgroundColor = MWHexColor(@"#D8D8D8");
    [self.view addSubview:_smsCodeLineView];
    
    _countDownButton = [JKCountDownButton buttonWithType:UIButtonTypeCustom];
    _countDownButton.titleLabel.font = MWFont(10);
    [_countDownButton setTitle:[[MWConfigManager manager] objectForKey:@"get_code" defaultValue:@"获取验证码"] forState:UIControlStateNormal];
    [_countDownButton setTitleColor:MWHexColor(@"#1E1934") forState:UIControlStateNormal];
    [self.view addSubview:_countDownButton];
    
    MWWeakSelf
    [_countDownButton countDownButtonHandler:^(JKCountDownButton*sender, NSInteger tag) {
        NSString *phone = weakSelf.phoneTF.text;
        [self loadSmsCodeWithPhone:phone countryCode:_countryCode];
        
        [sender countDownChanging:^NSString *(JKCountDownButton *countDownButton,NSUInteger second) {
            NSString *title = [NSString stringWithFormat:@"%zds",second];
            return title;
        }];
        
        [sender countDownFinished:^NSString *(JKCountDownButton *countDownButton, NSUInteger second) {
            countDownButton.enabled = YES;
            return [[MWConfigManager manager] objectForKey:@"getcode_retry" defaultValue:@"重新获取验证码"];
        }];
    }];
    
    _submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _submitBtn.layer.cornerRadius = 5;
    _submitBtn.layer.masksToBounds = YES;
    _submitBtn.titleLabel.font = MWFont(14);
    [_submitBtn setTitle:[[MWConfigManager manager] objectForKey:@"define" defaultValue:@"确定"] forState:UIControlStateNormal];
    [_submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_submitBtn setBackgroundImage:[UIImage imageWithColor:MWHexColor(@"#1E1934")] forState:UIControlStateNormal];
    [_submitBtn addTarget:self action:@selector(submitAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_submitBtn];
}

- (void)layoutPageSubviews {
    [_hintLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(90);
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
    }];
    
    [_phoneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(16);
        make.centerY.equalTo(self.phoneTF);
        make.width.equalTo(@80);
        make.height.equalTo(@20);
    }];
    [_phoneBtn setIconInRightWithSpacing:5];
       
    [_phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.hintLB.mas_bottom).offset(30);
        make.left.equalTo(self.phoneBtn.mas_right).offset(10);
        make.right.equalTo(self.view).offset(-16);
    }];
    
    [_phoneLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.phoneTF.mas_bottom).offset(7);
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
        make.height.equalTo(@0.5);
    }];
    
    [_smsCodeTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.phoneLineView.mas_bottom).offset(25);
        make.left.equalTo(self.view).offset(16);
    }];
    
    [_countDownButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-16);
        make.centerY.equalTo(self.smsCodeTF);
    }];
    
    [_smsCodeLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.smsCodeTF.mas_bottom).offset(7);
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
        make.height.equalTo(@0.5);
    }];
    
    [_submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.smsCodeLineView.mas_bottom).offset(100);
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
        make.height.equalTo(@44);
    }];
}

@end
